package com.example.huichaoliu;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.huichaoliu.adapters.BagAdapter;
import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.rederxu.farmer.BaseActivity;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;
import com.rederxu.tools.DisplayUtils;
import com.source.utils.Utils;

public class BagActivity extends BaseActivity implements OnClickListener {

	private SwipeMenuListView listView = null;
	private JSONArray jaData = new JSONArray();
	private BagAdapter bagAdapter = null;
	private TextView tv_num_tip = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_bag);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.left_button:
			finish();
			break;
		case R.id.btn_buy:

			if(jaData.length() == 0){
				showToast("购物袋还没有商品哦~");
				return;
			}
			JSONArray ja = new JSONArray();
			for(int i = 0; i < jaData.length();i++){
				try {
					if(jaData.getJSONObject(i).optBoolean("isSelected", false)){
						ja.put(jaData.getJSONObject(i));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ja.length() == 0){
				showToast("您还没有选中商品");
				return;
			}
			Intent i = new Intent(mContext,EnsureOrderActivity.class);
			i.putExtra("data", ja.toString());
			startActivity(i);
			break;
		case R.id.btn_share:
			startActivity(ShareActivity.class);
			break;
		}
	}

	@Override
	public void init() {
		((TextView) findViewById(R.id.title_text)).setText("购物袋");
		findViewById(R.id.left_button).setOnClickListener(this);
		findViewById(R.id.btn_buy).setOnClickListener(this);
		findViewById(R.id.btn_share).setOnClickListener(this);
		tv_num_tip = (TextView) findViewById(R.id.tv_num_tip);
		listView = (SwipeMenuListView) findViewById(R.id.listView);
		
		// step 1. create a MenuCreator
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				SwipeMenuItem deleteItem = new SwipeMenuItem(
						getApplicationContext());
				// set item width
				deleteItem.setWidth(DisplayUtils.dipToPx(mContext, 40));
				// set a icon
				deleteItem.setIcon(R.drawable.ic_garbage);
				// add to menu
				menu.addMenuItem(deleteItem);
			}
		};
		// set creator
		listView.setMenuCreator(creator);
		// step 2. listener item click event
		listView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				try{
					JSONObject jo = new JSONObject();
					jo.put("userId", Config.getUserId(mContext));
					jo.put("shoppingcarId", bagAdapter.getShoppingCarId(position));
//					Log.e("jo.toString()", jo.toString());
					Bundle bundle = new Bundle();
					bundle.putString("request", Utils.requestParamsEncode(jo));
					HTTPRequest.POST(mContext, Config.REQUEST_URLS.URL_SHOPPING_BAG_DELETE, bundle, new onRequestOverListener() {
						@Override
						public void onRequestOver(String result) {
							try{
								JSONObject jo = new JSONObject(result);
								if(jo.getInt("code") != 0){
									showToast(jo.getString("message"));
									return;
								}
								initData();
							}catch(JSONException e){
								e.printStackTrace();
							}
						}
					});
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void initData() {
		try{
			JSONObject jo = new JSONObject();
			jo.put("userId", Config.getUserId(mContext));
			jo.put("pageno", 1);
			jo.put("pagesize", 100);
			Bundle bundle = new Bundle();
			bundle.putString("request", Utils.requestParamsEncode(jo));
			HTTPRequest.POST(this, Config.REQUEST_URLS.URL_SHOPPING_BAG_LIST, bundle, new onRequestOverListener() {
				@Override
				public void onRequestOver(String result) {
					try{
						JSONObject jo = new JSONObject(result);
						if(jo.getInt("code") != 0){
							showToast(jo.getString("message"));
							return;
						}
						jaData = jo.getJSONArray("response");
						bagAdapter = new BagAdapter(mContext,jaData);
						listView.setAdapter(bagAdapter);
						if(jaData.length() == 0){
							tv_num_tip.setVisibility(View.VISIBLE);
						}else{
							tv_num_tip.setVisibility(View.GONE);
						}
					}catch(JSONException e){
						e.printStackTrace();
					}
				}
			});
		}catch(Exception e){
			
		}
	}

	@Override
	public void onNetworkChanged(boolean isMobNetConnected,
			boolean isWifiConnected) {
		// TODO Auto-generated method stub

	}
}
